package com.feonix.common.permission;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.method.HandlerMethod;

import com.feonix.common.annotation.Permission;
import com.feonix.common.util.StringUtil;
import com.feonix.complat.entity.Right;
import com.feonix.support.CurrentUser;

public class PermissionUtil {

	/**
	 * 是否需要判断权限
	 * 
	 * @param handler
	 * @return
	 */
	public static boolean isNeedCheck(Object handler) {
		boolean isNeed = false;
		if (handler instanceof HandlerMethod) {
			HandlerMethod handlerMethod = (HandlerMethod) handler;
			Permission classPermission = handlerMethod.getBeanType().getAnnotation(Permission.class);
			Permission methodPermission = handlerMethod.getMethod().getAnnotation(Permission.class);
			if ((classPermission != null) || (methodPermission != null)) {
				isNeed = true;
			}
		}
		return isNeed;
	}

	/**
	 * 获得权限适配器实例
	 * 
	 * @param handler
	 * @return
	 */
	public static PermissionConfig getPermissionConfig(Object handler) {
		PermissionConfig permissionConfig = null;
		if (handler instanceof HandlerMethod) {
			permissionConfig = new PermissionConfig();
			HandlerMethod handlerMethod = (HandlerMethod) handler;
			com.feonix.common.annotation.PermissionConfig beanConfig = handlerMethod.getBeanType()
					.getAnnotation(com.feonix.common.annotation.PermissionConfig.class);
			com.feonix.common.annotation.PermissionConfig methodConfig = handlerMethod.getMethod()
					.getAnnotation(com.feonix.common.annotation.PermissionConfig.class);
			if (beanConfig != null) {
				permissionConfig.setAdapter(beanConfig.adapter());
				permissionConfig.setValidModal(beanConfig.validModal());
			}
			if (methodConfig != null) {
				permissionConfig.setAdapter(methodConfig.adapter());
				permissionConfig.setValidModal(methodConfig.validModal());
			}
		}
		return permissionConfig;
	}

	/**
	 * 获得PermissionStructure
	 * 
	 * @param handler
	 * @return
	 */
	public static PermissionStructure getStructure(Object handler) {
		PermissionStructure permissionStructure = null;
		if (handler instanceof HandlerMethod) {
			HandlerMethod handlerMethod = (HandlerMethod) handler;
			Permission controllerPermission = handlerMethod.getBeanType().getAnnotation(Permission.class);
			Permission methodPermission = handlerMethod.getMethod().getAnnotation(Permission.class);
			permissionStructure = organizePermissionStructure(controllerPermission, methodPermission);
		}
		return permissionStructure;
	}

	/**
	 * 获取当前访问的url的权限标识
	 * 
	 * @param controllerPermission
	 * @param methodPermission
	 * @return
	 */
	private static PermissionStructure organizePermissionStructure(Permission controllerPermission,
			Permission methodPermission) {
		PermissionStructure permissionStructure = null;
		if ((controllerPermission != null) || (methodPermission != null)) {
			permissionStructure = new PermissionStructure();
			if (controllerPermission != null) {
				organizePermissionStructure(controllerPermission, permissionStructure, true);
			}
			if (methodPermission != null) {
				organizePermissionStructure(methodPermission, permissionStructure, false);
			}
		}
		return permissionStructure;
	}

	/**
	 * 获取权限结构体
	 * 
	 * @param permission
	 * @param structure
	 * @param isDefault
	 */
	private static void organizePermissionStructure(Permission permission, PermissionStructure structure,
			boolean isDefault) {
		String str1 = permission.module();
		String str2 = permission.function();
		String str3 = permission.params();
		String str4 = permission.rootPath();
		if (StringUtil.isNotEmpty(str1)) {
			structure.setModuleId(str1);
		}
		if (StringUtil.isNotEmpty(str2)) {
			structure.setFunctionId(str2);
		}
		if (StringUtil.isNotEmpty(str3)) {
			structure.setParams(str3);
		}
		if (StringUtil.isNotEmpty(str4)) {
			structure.setRootPath(str4);
		}
		switch (permission.allowedAdmin().ordinal() + 1) {
		case 1:
			structure.setAllowedAdmin(Boolean.valueOf(true));
			break;
		case 2:
			structure.setAllowedAdmin(Boolean.valueOf(false));
			break;
		case 4:
			break;
		case 3:
		default:
			if (isDefault) {
				structure.setAllowedAdmin(Boolean.valueOf(true));
			}
			break;
		}

		switch (permission.allowedGroup().ordinal() + 1) {
		case 1:
			structure.setAllowedGroup(Boolean.valueOf(true));
			break;
		case 2:
			structure.setAllowedGroup(Boolean.valueOf(false));
			break;
		case 4:
			break;
		case 3:
		default:
			if (!(isDefault)) {
				structure.setAllowedGroup(Boolean.valueOf(false));
			}
			break;
		}
	}

	/**
	 * 组织权限特征字符串
	 * 
	 * @param user
	 */
	public static void organizePermissionKey(CurrentUser user) {
		List<Right> rightList = user.getRightList();
		if (CollectionUtils.isNotEmpty(rightList)) {
			PermissionKey permissionKey = null;
			for (Right right : rightList) {
				permissionKey = organizePermissionKey(right.getModuleId(), right.getFunctionId(), right.getDynamicId(),
						null, null);
				if (permissionKey != null)
					user.getPermissions().add(permissionKey.getToken());
			}
		}
	}

	/**
	 * 组织权限标识
	 * 
	 * @param module
	 * @param function
	 * @param dynamic
	 * @param allowedAdmin
	 * @param allowedGroup
	 * @return
	 */
	public static PermissionKey organizePermissionKey(String module, String function, String dynamic,
			Boolean allowedAdmin, Boolean allowedGroup) {
		String token = "";
		PermissionKey permissionKey = new PermissionKey();
		if (allowedAdmin != null) {
			permissionKey.setAllowedAdmin(allowedAdmin);
		}
		if (allowedGroup != null) {
			permissionKey.setAllowedGroup(allowedGroup);
		}
		if (StringUtil.isNotEmpty(module)) {
			token = token + module;
			if (StringUtil.isNotEmpty(function)) {
				token = token + "_" + function;
			}
			if (StringUtil.isNotEmpty(dynamic)) {
				token = token + "_" + dynamic;
			}
		}
		permissionKey.setToken(token);
		return permissionKey;
	}
}
